package cn.lsnu.modules.check.controller;

import cn.lsnu.common.interfacetools.interfacecontroller.BaseController;
import cn.lsnu.common.interfacetools.interfaceentity.Page;
import cn.lsnu.common.sys.entity.MenuEntity;
import cn.lsnu.common.sys.service.MenuService;
import cn.lsnu.modules.check.entity.CheckTaskEntity;
import cn.lsnu.modules.check.service.CheckTaskService;
import cn.lsnu.utils.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping({"/app/checktask","/task/check"})
public class CheckTaskController extends BaseController{

    @Autowired
    private CheckTaskService checkTaskService;

    @Autowired
    private MenuService menuService;

    @RequestMapping({"/page","/listjson"})
    public
    @ResponseBody Page<CheckTaskEntity> pages(CheckTaskEntity page)throws  Exception{
        Page<CheckTaskEntity> pageList = checkTaskService.findPage(page);
        return pageList;
    }

    @RequestMapping("/taskdetil")
    public
    @ResponseBody CheckTaskEntity taskDetil(String key)throws  Exception{
        return checkTaskService.get(key);
    }

    @RequestMapping("/save")
    public void save(CheckTaskEntity entity)throws Exception{
        checkTaskService.save(entity);
    }

    @RequestMapping({"/lists"})
    public
    @ResponseBody List<CheckTaskEntity> lists(CheckTaskEntity entity)throws  Exception{
        List<CheckTaskEntity> lists = checkTaskService.findList(entity);
        return lists;
    }


    @RequestMapping("/list")
    public String listInfos(Model model, HttpSession session, MenuEntity entity)throws Exception{
        String firstTitle = entity.getFirstTitle();
        //每个-list页面的id
        model.addAttribute("pageId",entity.getMenuId());
        //获取页面的所有信息
        String userCode = (String) session.getAttribute(Global.SESSION_ID);
        entity.setUserCode(userCode);
        entity = menuService.getPageMenu(entity);
        entity.setFirstTitle(firstTitle);
        entity.setSecondTitle("信息管理列表");
        model.addAttribute("pageMenuMsg",entity);
        return "webpage/modules/task/checkrecord-list";
    }


    @RequestMapping("/updmsg")
    public String updMsg(String key,Model model)throws Exception{
        CheckTaskEntity  entity = checkTaskService.get(key);
        model.addAttribute("updcheck",entity);
        return "webpage/modules/task/checkrecord-upd";
    }

    @RequestMapping("/upd")
    public void upd(CheckTaskEntity entity)throws Exception{
        checkTaskService.update(entity);
    }

    @RequestMapping(value = "/del",method= RequestMethod.POST)
    public
    @ResponseBody CheckTaskEntity
    delete(CheckTaskEntity entity)throws Exception{
        checkTaskService.delete(entity);
        entity.setCode(Global.STATUE_SUCCESS);
        return entity;
    }


    @RequestMapping(value = "/addCount")
    public
    @ResponseBody
    List<CheckTaskEntity> addCount(CheckTaskEntity entity)throws Exception{
        return checkTaskService.addressCount(entity);
    }



}
